package com.zhuimeng.mysql_rocketmq.listener;

import com.zhuimeng.mysql_rocketmq.dao.SkillOrderRepository;
import com.zhuimeng.mysql_rocketmq.pojo.SkillOrder;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(topic = "PAYMESSAGE",consumerGroup = "PAYCONSUMER")
public class PayRocketListener implements RocketMQListener<String>{
    @Autowired
    SkillOrderRepository skillOrderRepository;
    @Override
    public void onMessage(String s) {
        SkillOrder skillOrder = new SkillOrder();
        skillOrder.setUserId(s);
        skillOrder.setOrderStatus("未支付");
        final List<SkillOrder> orderList = skillOrderRepository.findAll(Example.of(skillOrder));
        if (orderList != null && orderList.size()>0){
            final SkillOrder order = orderList.get(0);
            order.setOrderStatus("已支付");
            skillOrderRepository.saveAndFlush(order);
        }
    }
}
